#pragma once
#include<iostream>
#include<pthread.h>

namespace MutexModule
{
    

    class Mutex
    {
    public:
        Mutex(const Mutex& mutex) = delete;
        Mutex operator=(Mutex& mutex) = delete;
        Mutex()
        {
            pthread_mutex_init(&_mutex , nullptr);
            //std::cout << "mutex 初始化成功" << std::endl;
        }

        void Lock()
        {
            pthread_mutex_lock(&_mutex);
            //std::cout << "mutex lock成功" << std::endl;
        }
        void UnLock()
        {
            pthread_mutex_unlock(&_mutex);
            //std::cout << "mutex unlock成功" << std::endl;
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
            //std::cout << "mutex destroy成功" << std::endl;
        }
        pthread_mutex_t* LockPtr()
        {
            return &_mutex;
        }
    private:
        pthread_mutex_t _mutex;
    };
    class MutexGuard
    {
    public:
        MutexGuard(Mutex& lock)
        :_lock(lock)
        {
            _lock.Lock();
        }

        ~MutexGuard()
        {
            _lock.UnLock();
        }
    private:
        Mutex& _lock;
    };
}